<?php


namespace app\user\controller;

use app\common\controller\Common;
use app\common\model\Category;
use app\common\model\Management;
use geohash\Geohash;
use think\cache\driver\Redis;
use think\Db;
use think\facade\Log;

class Merchant extends Common
{
    //服务列表
    public function merchantlists()
    {
        $cate_id = request()->param('cate_id');
        $area_id = request()->param('area_id');
        $attr = request()->param('attr');
        $value = request()->param('value');//asc desc
        $data = request()->param('data');
        $page = $this->getPage();
        $pagesize = $this->getPageSize();
        $min = ($page - 1) * $pagesize;
        $max = $pagesize;
        if(is_null($data)||is_null($value)||is_null($cate_id)) return $this->errJson('参数错误');
        $data = json_decode($data);
        $lat = $data->latitude;//伟度
        $long = $data->longitude;//经
        //redis geo处理地理位置
        $redis = new \Redis();
        if ($redis->connect('127.0.0.1', 6379)) {
            $param = array('georadius', 'store', $long, $lat, '30', 'km', 'WITHDIST', $value);
            $retStores = call_user_func_array(array($redis, 'rawCommand'), $param);
            $merids = [];
            if (is_array($retStores) && count($retStores) > 0) {
                foreach ($retStores as $key => $val) {
                    $merids[$key] = $val[0];
                }
                $merids = implode(',', $merids);
            } else {
                return $this->errJson('30公里内暂时无商家');
            }

        }
        //----------
        //默认
        if ($area_id == "" || is_null($area_id) || trim($area_id) == "150100" || trim($area_id) == "1501001") $area_id = 150;
        if ($attr == "" || is_null($attr) || $value == "" || is_null($value)) {
            $sql = "SELECT * FROM merchant WHERE cate_id=" . $cate_id . " AND area_id LIKE '" . $area_id . "%' AND merid IN(" . $merids . ")";
            $count = Db::query($sql);
            $retMer = Db::query($sql . "limit " . $min . "," . $max);
            if (count($retMer) == 0) return $this->errJson('已经到底了');
            foreach ($retMer as $k => $v) {
                foreach ($retStores as $ke => $vv) {
                    if ($v['merid'] == $vv[0]) {
                        $retMer[$k]['len'] = $vv[1];
                    }
                }
            }
            return $this->listJson($retMer, count($count));
        }


        //按距离排序
        if (isset($attr) && $attr == 'location' && $data != "") {

            $sql = "SELECT * FROM merchant WHERE cate_id=" . $cate_id . " AND area_id LIKE '" . $area_id . "%' AND merid IN(" . $merids . ") order by find_in_set(merid,'" . $merids . "')";
            $count = Db::query($sql);
            $retMer = Db::query($sql . " limit " . $min . "," . $max);
            if (count($retMer) == 0) return $this->errJson('已经到底了');
            foreach ($retMer as $k => $v) {
                foreach ($retStores as $ke => $vv) {
                    if ($v['merid'] == $vv[0]) {
                        $retMer[$k]['len'] = $vv[1];
                    }
                }
            }
            return $this->listJson($retMer, count($count));
        }
        //按折扣
        $sql = "SELECT * FROM merchant WHERE cate_id=" . $cate_id . " AND area_id LIKE '" . $area_id . "%' AND merid IN(" . $merids . ") ORDER BY " . $attr . " " . $value;
        $count = Db::query($sql);
        $retMer = Db::query($sql . " limit " . $min . "," . $max);
        if (count($retMer) == 0) return $this->errJson('已经到底了');
        foreach ($retMer as $k => $v) {
            foreach ($retStores as $ke => $vv) {
                if ($v['merid'] == $vv[0]) {
                    $retMer[$k]['len'] = $vv[1];
                }
            }
        }
        return $this->listJson($retMer, count($count));
    }

    //服务详情
    public function show()
    {
        $merid = request()->param('merid');
        $merid = \app\common\model\Merchant::get($merid);
        if ($merid) return $this->okJson('ok', $merid);

        return $this->errJson('暂无数据');
    }

}